package com.corpcredit.webapp.action;

import java.io.Serializable;
import java.util.Date;

import com.corpcredit.Constants;
import com.corpcredit.model.Pdxm;
import com.corpcredit.webapp.action.BasePage;
import com.corpcredit.service.GenericManager;

public class PdxmForm extends BasePage implements Serializable {
    private GenericManager<Pdxm, Long> pdxmManager;
    private Pdxm pdxm = new Pdxm();
    private Long id;

    public void setPdxmManager(GenericManager<Pdxm, Long> pdxmManager) {
        this.pdxmManager = pdxmManager;
    }

    public Pdxm getPdxm() {
        return pdxm;
    }

    public void setPdxm(Pdxm pdxm) {
        this.pdxm = pdxm;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String delete() {
        pdxmManager.remove(pdxm.getId());
        addMessage("pdxm.deleted");

        return "list";
    }

    public String edit() {
        // Comparison to zero (vs. null) is required with MyFaces 1.2.2, not with previous versions
        if (id != null && id != 0) {
            pdxm = pdxmManager.get(id);
        } else {
            pdxm = new Pdxm();
        }

        return "edit";
    }

    public String save() {
        boolean isNew = (pdxm.getId() == null || pdxm.getId() == 0);
        pdxm.setUsername(getRequest().getRemoteUser());
        pdxm.setOptdate(new Date());
        pdxm.setOperation(isNew?Constants.OPERATION_ADD:Constants.OPERATION_UPDATE);
        
        
        pdxmManager.save(pdxm);

        String key = (isNew) ? "pdxm.added" : "pdxm.updated";
        addMessage(key);

        if (isNew) {
            return "list";
        } else {
        	 this.id=this.pdxm.getId();
             return edit();
        }
    }
} 